﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TDT.Helper;
using System.Web.Mvc;
using System.Collections;
using System.Web.UI.WebControls;
using System.Reflection;
using System.ComponentModel;
using System.Net.Mail;
using System.Net;

namespace TDT.Land.Utilities
{
    /// <summary>
    /// Lớp commons : dùng để chứa các hàm dùng chung
    /// </summary>
    public class Commons
    {
        public static string GetAddress(string provinceName, string districtName, string wardName, string address)
        {

            if (!string.IsNullOrWhiteSpace(wardName))
            {
                address += (string.IsNullOrWhiteSpace(address) ? string.Empty : ", ") + wardName;
            }

            if (!string.IsNullOrWhiteSpace(districtName))
            {
                address += (string.IsNullOrWhiteSpace(address) ? string.Empty : ", ") + districtName;
            }

            if (!string.IsNullOrWhiteSpace(provinceName))
            {
                address += (string.IsNullOrWhiteSpace(address) ? string.Empty : ", ") + provinceName;
            }

            return address;
        }

        public static bool IsRelease
        {
            get
            {
#if(IsRelease)
                    return true;
#else
                return false;
#endif
            }
        }

        /// <summary>Chuyển string thành Guid</summary>
        /// <param name="guidValue">Giá trị chuổi guid</param>
        /// <returns></returns>
        public static Guid GetGuid(string guidValue)
        {
            var returnValue = Guid.Empty;
            Guid.TryParse(guidValue, out returnValue);
            return returnValue;
        }

        /// <summary>
        /// Kiểm tra chuổi có phải là kiểu Guid không
        /// </summary>
        /// <param name="guidValue"></param>
        /// <returns></returns>
        public static bool IsGuid(string guidValue)
        {
            var returnValue = Guid.Empty;
            Guid.TryParse(guidValue, out returnValue);
            return returnValue != Guid.Empty;
        }

        #region Mã hoá password

        /// <summary>Mã hoá password</summary>
        /// <param name="password"></param>
        /// <returns></returns>
        public static string PasswordEncryption(string password)
        {
            return Library.Instance.MD5(password);

        }
        #endregion

        #region selectList
        public static List<SelectListItem> ConvertToSelectListItems(IList ListData, string ColumeValueName, string ColumeTextName)
        {
            return ConvertToSelectListItems(ListData, ColumeValueName, ColumeTextName, string.Empty);
        }
        public static List<SelectListItem> ConvertToSelectListItems(IList ListData, string ColumeValueName, string ColumeTextName, string SelectedItem)
        {
            List<SelectListItem> lstItem = new List<SelectListItem>();
            SelectListItem selItemFirst = new SelectListItem();
            lstItem.Add(selItemFirst);
            foreach (var item in ListData)
            {
                if (item.GetType().GetProperty(ColumeValueName).GetValue(item, null) == null || item.GetType().GetProperty(ColumeTextName).GetValue(item, null) == null)
                {
                    continue;
                }
                string value = item.GetType().GetProperty(ColumeValueName).GetValue(item, null).ToString();
                string text = item.GetType().GetProperty(ColumeTextName).GetValue(item, null).ToString();
                SelectListItem selItem = new SelectListItem();
                selItem.Text = text;
                selItem.Value = value;
                if (value.ToLower().Equals(SelectedItem.ToLower()))
                {
                    selItem.Selected = true;
                }
                lstItem.Add(selItem);
            }
            return lstItem;
        }

        public static List<SelectListItem> ConvertToSelectListItems(Type EnumType)
        {
            return ConvertToSelectListItems(EnumType, string.Empty,false);
        }
        public static List<SelectListItem> ConvertToSelectListItems(Type EnumType, bool IsNotNull)
        {
            return ConvertToSelectListItems(EnumType, string.Empty, IsNotNull);
        }
        public static List<SelectListItem> ConvertToSelectListItems(Type EnumType, string SelectedItem, bool IsNotNull)
        {
            List<SelectListItem> lstItem = new List<SelectListItem>();
            if (!IsNotNull)
            {
                SelectListItem selItemFirst = new SelectListItem();
                lstItem.Add(selItemFirst);
            }
            var names = Enum.GetNames(EnumType);
            foreach (var name in names)
            {
                string value = name.ToString();
                string text = name.ToString().Translate();
                SelectListItem selItem = new SelectListItem();
                selItem.Text = text;
                selItem.Value = value;
                if (value.ToLower().Equals(SelectedItem.ToLower()))
                {
                    selItem.Selected = true;
                }
                lstItem.Add(selItem);
            }
            return lstItem;
        }
        #endregion
                
        /// <summary>
        /// Lay danh sach enum thanh selectListItem đã co dịch
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static List<SelectListItem> GetSelectList<T>() 
        {
            List<SelectListItem> list = System.Enum.GetValues(typeof(T))
                              .Cast<T>()
                              .Select(x => new SelectListItem { Value = x.ToString(), Text = Library.Instance.GetEnumDescription(x) })
                              .ToList();
            return list;
        }

        public static string EnumTranslate<T>(string enumText) {
            T enumType = (T)Enum.Parse(typeof(T), enumText);
            return Library.Instance.GetEnumDescription<T>(enumType);
        }

        public static List<SelectListItem> GetSelectList<T>(string enumValue)
        {
            List<SelectListItem> list = System.Enum.GetValues(typeof(T))
                              .Cast<T>()
                              .Select(x => new SelectListItem 
                              {
                                  Value = x.ToString(),
                                  Text = Library.Instance.GetEnumDescription(x) ,
                                  Selected = x.ToString() == enumValue
                              })
                              .ToList();
            return list;
        }

        

        #region ValidateModel
        public static string ValidateModelState(ModelStateDictionary modelState)
        {
            if (!modelState.IsValid)
            {
                var lstError = modelState.Where(m => m.Value.Errors != null && m.Value.Errors.Count > 0).ToList();
                string Error = string.Empty;
                foreach (var item in lstError)
                {
                    if (item.Key.ToUpper() == "ID")
                        continue;
                    var lstErrorItem = item.Value.Errors.ToList();
                    foreach (var errorItem in lstErrorItem)
                    {
                        Error += errorItem.ErrorMessage + "; ";
                    }
                }
                if (Error != string.Empty)
                {
                    Error = Error.Substring(0, Error.Length - 2);
                    return Error;
                }
            }
            return string.Empty;
        }
        #endregion
    }
      

}
